package cn.mujiankeji.mbrowser.功能.播放框.核心

import android.content.Context
import androidx.media3.database.StandaloneDatabaseProvider
import androidx.media3.datasource.cache.LeastRecentlyUsedCacheEvictor
import androidx.media3.datasource.cache.SimpleCache
import java.io.File

object CacheManager {
    @Volatile
    private var simpleCache: SimpleCache? = null
    
    // 增加缓存大小到2GB
    private const val MAX_CACHE_SIZE = 2L * 1024 * 1024 * 1024 // 2GB

    fun getCache(context: Context): SimpleCache {
        return simpleCache ?: synchronized(this) {
            simpleCache ?: SimpleCache(
                File(context.cacheDir, "video_cache"),
                LeastRecentlyUsedCacheEvictor(MAX_CACHE_SIZE),
                StandaloneDatabaseProvider(context)
            ).also { simpleCache = it }
        }
    }
    
    // 清除缓存的方法
    fun clearCache() {
        simpleCache?.let {
            try {
                it.keys.forEach { key ->
                    it.removeResource(key)
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }
} 